﻿
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class MathTool
{
    public static Vector2 AngleToVector(float angle)
    {
        return new Vector2(Mathf.Cos(angle/180*Mathf.PI), Mathf.Sin(angle/180*Mathf.PI));
    }
    public static float VectorToAngle(Vector2 vector)
    {
        return Mathf.Atan2(vector.y, vector.x) / Mathf.PI * 180;
    }
    public static Vector2 Project(Vector2 a,Vector2 norm)
    {
        return norm.normalized * Vector2.Dot(a, norm)/norm.magnitude;
    }
    public static void DrawCube(Vector2 pos,Vector2 siz,float angle)
    {
        List<Vector2> p = new List<Vector2>();
        p.Add(pos + siz / 2);
        p.Add(pos - siz / 2);
        p.Add(new Vector2(p[0].x, p[1].y));
        p.Add(new Vector2(p[1].x, p[0].y));
        var tp = p[1];
        p[1] = p[2];
        p[2] = tp;
        for(int i = 0; i < 4; i++)
        {
            p[i] = UnsafeTool.TurnAngle(p[i]-pos, angle / 180 * Mathf.PI)+pos;
        }
        for(int i = 0; i < 4; i++)
        {
            Gizmos.DrawLine(p[i], p[(i + 1) % 4]);
        }
    }
}
